from django.shortcuts import render
from django.http import HttpResponse,JsonResponse
from django.views import View

# Create your views here.
def index(request,*args,**kwargs):
    return HttpResponse('index')

class IndexView(View):
    def get(self,request,*args,**kwargs):
        return HttpResponse("index view!")

def login(request,*args,**kwargs):
    if request.method == "GET":
        return HttpResponse("登录页面")
    elif request.method == "POST":
        return HttpResponse("验证功能")
    else:
        pass
    return HttpResponse("")

class LoginView(View):
    def get(self,request,*args,**kwargs):
        return HttpResponse("登录页面")
    def post(self,request,*args,**kwargs):
        return HttpResponse("验证功能")

def user(request,*args,**kwargs):
    return HttpResponse("")

class UserView_v1(View):
    http_method_names = ['get', 'post', 'put', 'patch', 'delete', 'head', 'options', 'trace','list']
    def get(self,request,*args,**kwargs):
        return HttpResponse("用户信息")
    def post(self,request,*args,**kwargs):
        return HttpResponse("修改用户信息")
    def delete(self,request,*args,**kwargs):
        return HttpResponse("删除用户信息")
    def put(self,request,*args,**kwargs):
        return HttpResponse("新增用户信息")
    def list(self,request,*args,**kwargs):
        return HttpResponse("获取用户列表")

from django.contrib.auth.models import User
class UserView(View):
    def get(self,request,*args,**kwargs):
        per = 10
        try:
            page = int(request.GET.get("page"))
        except:
            page = 1
        if page < 1:
            page = 1
        end = page * per
        start = end - per
        queryset = User.objects.all()[start:end]
        data = [{"id":user.id,"username":user.username,"email":user.email} for user in queryset]
        return JsonResponse(data,safe=False)

from django.core.paginator import Paginator
from django.db import IntegrityError
import logging
logger = logging.getLogger(__name__)

class UserViewV2(View):
    def get(self,request,*args,**kwargs):
        queryset = User.objects.all()
        pagenator = Paginator(queryset,10)
        try:
            page = int(request.GET.get("page"))
        except:
            page = 1
        if page < 1:
            page = 1
        page = pagenator.page(page)
        data = [{"id": user.id, "username": user.username, "email": user.email} for user in page.object_list]
        return JsonResponse(data, safe=False)

    def post(self,request,*args,**kwargs):
        logger.debug("创建用户")
        #获取提交过来的参数
        #username = request.POST.get("username")
        #password = request.POST.get("password")
        #email= request.POST.get("email")
        data = request.POST.dict()
        #print(data)
        #验证参数（所有的参数不为空即可）
        #if not username or not password or not email:
        #   return JsonResponse({"errmsg":"参数有误"})
        try:
            user = User.objects.create_user(**data)
        except IntegrityError:
            return JsonResponse({"errmsg":"用户已存在"})
        return JsonResponse({"id":user.id,"username":user.username,"email":user.email})

